/*
 * TablaDBF.java
 *
 * Created on 16 de agosto de 2007, 11:39
 */

package utiles;
import java.sql.*;  
 import java.io.*;  
 import com.hxtt.sql.*;
 import java.rmi.*;
 import java.rmi.registry.*;
 import java.rmi.registry.LocateRegistry;
/**
 *
 * @author  admin
 */
public class TablaDBF extends javax.swing.JFrame {
    funcioncliente.FuncionesControlAcceso fca;
    String conexao = "com.hxtt.sql.dbf.DBFDriver"; 
    utiles.ModificableTableModel modelo;
    Statement stmt;
    Connection conn;
    DatosTablaDBF dbb;
    utiles.conexionRmiJdbc conex;
    /** Creates new form TablaDBF */
    ResultSet result;
    public TablaDBF() {
        initComponents();
        try{
            
        System.setProperty("java.security.policy", "c:/pruebaRMI/java.policy");
        System.setProperty ("java.rmi.server.codebase","http://192.168.23.6/");
        
        if (System.getSecurityManager() == null){
 		System.setSecurityManager(new RMISecurityManager());
 		}
            fca=( funcioncliente.FuncionesControlAcceso)java.rmi.Naming.lookup("//192.168.23.6:1234/rmi");
            
            
        }catch(Exception e){
            e.printStackTrace();
        }
        
        try{
            Class.forName(conexao).newInstance();
            String url = "jdbc:DBF:/c:/dbf/";
            conn = DriverManager.getConnection(url);
            stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_SENSITIVE,
                                                          java.sql.ResultSet.CONCUR_UPDATABLE);
        }catch(Exception e){}
        conex=new utiles.conexionRmiJdbc("192.168.23.6","SQL2005","RecursosHumanos","administrador","administrador1234",true);
    }
    
    
    void imprimir_dato_tabla(ResultSetMetaData rsmd){
        try{
        int numcol=rsmd.getColumnCount();
        dbb=new DatosTablaDBF(new javax.swing.JFrame(), false);
        javax.swing.table.DefaultTableModel modelo=(javax.swing.table.DefaultTableModel)dbb.jtdatostabla.getModel();
        
        
        for(int i=0;i<numcol;i++){
           boolean nul=false;
           if(rsmd.isNullable(i+1)==rsmd.columnNullable){
               nul=true;
           }
          
           modelo.insertRow(i,new Object[] {rsmd.getColumnName(i+1),
                                            tipdato(rsmd.getColumnType(i+1)),
                                            rsmd.getScale(i+1),
                                            null});
        
        }
        
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
    void consultar(String a){
        try{
            ResultSet res=stmt.executeQuery(a);
            result=res;
            imprimir_dato_tabla(res.getMetaData());
            modelo=new utiles.ModificableTableModel(res);
            jtTablaDBF.setModel(modelo);
            pack();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    String tipdato(int tip){
        String ret="";
        switch (tip){
            case java.sql.Types.VARCHAR :   ret="varchar";
                                            break;
            case java.sql.Types.VARBINARY:  ret="varbinary";
                                            break;
            case java.sql.Types.TINYINT:  ret="tinyint";
                                            break;
           
            case java.sql.Types.TIMESTAMP:  ret="timestamp";
                                            break;

            case java.sql.Types.TIME:  ret="time";
                                                        break;
            case java.sql.Types.BIT:  ret="bit";
                                                        break;
            case java.sql.Types.BLOB:  ret="blob";
                                                        break;
            case java.sql.Types.CHAR:  ret="char";
                                                        break;
            case java.sql.Types.DATE:  ret="date";
                                                        break;
            case java.sql.Types.DOUBLE:  ret="double";
                                                        break;
            case java.sql.Types.FLOAT:  ret="float";
                                                        break;
            case java.sql.Types.INTEGER:  ret="integer";
                                                        break;
            case java.sql.Types.DECIMAL:  ret="decimal";
                                                        break;
            case java.sql.Types.NUMERIC:  ret="numeric";
                                                        break;
            case java.sql.Types.NVARCHAR:  ret="ncarchar";
                                                        break;                                            
        }
        
        return ret;
    }
    
    void cerrar(){
        try{
            
            conn.close(); 
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jtTablaDBF = new javax.swing.JTable();
        jtfConsulta = new javax.swing.JTextField();
        jbConsultar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        jtTablaDBF.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtTablaDBF);

        jbConsultar.setText("migrar");
        jbConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConsultarActionPerformed(evt);
            }
        });

        jButton1.setText("Salir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Informacion");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Consultar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("imprimir");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1032, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jtfConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 929, Short.MAX_VALUE)
                            .addGap(12, 12, 12)
                            .addComponent(jButton3)
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbConsultar)
                        .addGap(20, 20, 20)
                        .addComponent(jButton1)
                        .addGap(40, 40, 40)
                        .addComponent(jButton2)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jbConsultar)
                    .addComponent(jButton4))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
        try{
            jtTablaDBF.print();
        }catch(Exception e){
            
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
        consultar(jtfConsulta.getText().trim());
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
        dbb.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
        conex.cerrar();
        cerrar();
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jbConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConsultarActionPerformed
// TODO add your handling code here:
       // consultar("select cod,id,cta,cta_p,ctac1,con,con1+' '+con2,usado," +
         //       "porc,ir5ta,ley,ries,pordias,fla,fijo,oblig,indem from PLCON");
        
       int contador=1;
        try{
            
        
            result=stmt.executeQuery("select cod,id,cta,cta_p,ctac1,con,con1+' '+con2,usado," +
                "porc,ir5ta,ley,ries,pordias,fla,fijo,oblig,indem from PLCON");
            CallableStatement sen;
            result.last();
            int num=result.getRow();
            System.out.println(num);
            for(int i=0;i<num;i++){
                 Object nul=null;
        String id_co=null,id_ti_co=null,cuenta1=null,cuenta2=null,deslar=null,descor=null;
        float monto=0;
        boolean vigente,fijo=true,ir,ley,sctr,dias1,dias2,faltas,hextra,indem,grati=false;
        boolean bnul=false;
        float auxf;
        String aux,aux1;
        java.sql.Timestamp fec=null;
                result.absolute(i+1);
                id_co=result.getString(1).trim();
               try{ aux=result.getString(2).trim();
                   if(aux.equalsIgnoreCase("B")||aux.equalsIgnoreCase("I")){
                        id_ti_co="1";
                   }else if(aux.equalsIgnoreCase("G")){
                        grati=true;
                        id_ti_co="1";
                   }else  if((aux.equalsIgnoreCase("C"))|| (aux.equalsIgnoreCase("E"))){
                        id_ti_co="2";
                   }else  if(aux.equalsIgnoreCase("P")){
                        id_ti_co="3";
                   }
               }catch(Exception z1){
                   id_ti_co=null;
               }
                
               try{ 
               cuenta1=result.getString(3).trim();
               }catch(Exception z2){
                   cuenta1=null;
               }
               aux=null;
               try{
                   aux=result.getString(4).trim();
                   if(aux.equalsIgnoreCase("")){
                       aux=result.getString(5);

                   }
                   if(aux.equalsIgnoreCase(cuenta1)){
                           aux=null;
                       }
                   if(aux.equalsIgnoreCase(""))
                       aux=null;
               }catch(Exception efg){
                   aux=null;
               }
                cuenta2=aux;
                try{
                    deslar=result.getString(6).trim();
                }catch(Exception z12){
                    deslar=null;
                }
                try{
                    descor=result.getString(7).trim();
                }catch(Exception  essss){
                    descor=null;
                }
                try{
                    vigente=result.getBoolean(8);
                }catch(Exception efds){
                    vigente=false;
                }
                try{
                    auxf=result.getFloat(9);
                    if(auxf>0){
                        monto=auxf;
                        fijo=false;
                    }
                    
                }catch(Exception e45558874){
                    monto=0;
                }
              
                
                try{
                        aux=result.getString(10).trim();

                    if(aux.equalsIgnoreCase("S")){
                        ir=true;
                    }else{
                        ir=false;
                    }
                }catch(Exception e1){
                    ir=false;
                }
                    
                try{
                    aux=result.getString(11).trim();
                    if(aux.equalsIgnoreCase("S")){
                        ley=true;
                    }else{
                        ley=false;
                    }
                }catch(Exception e2){
                    ley=false;
                }
                
                try{
                     aux=result.getString(12).trim();
                    if(aux.equalsIgnoreCase("S")){
                        sctr=true;
                    }else{
                        sctr=false;
                    }
                }catch(Exception e3){
                    sctr=false;
                }
                try{
                 aux=result.getString(13).trim();
                if(aux.equalsIgnoreCase("S")){
                    dias1=true;
                }else{
                    dias1=false;
                }
                }catch(Exception e4){
                    dias1=false;
                }
                try{  
                aux=result.getString(14).trim();
                if(aux.equalsIgnoreCase("S")){
                    dias2=true;
                }else{
                    dias2=false;
                }
                }catch(Exception e5){
                    dias2=false;
                }
                
                try{
                    faltas=result.getBoolean(15);
                    faltas=!faltas;
                }catch(Exception e6){
                    faltas=false;
                }
                
                try{ 
                aux=result.getString(16).trim();
                if(aux.equalsIgnoreCase("S")){
                    hextra=true;
                }else{
                    hextra=false;
                }
                }catch(Exception e7){
                    hextra=false;
                }
                try{  
                aux=result.getString(17).trim();
                if(aux.equalsIgnoreCase("S")){
                    indem=true;
                }else{
                    indem=false;
                }
                }catch(Exception e8){
                    indem=false;
                }
                  if((id_co!=null) && (id_ti_co!=null)){
                  
            /*fca.migrar(id_co,id_ti_co,cuenta1,cuenta2,monto,fec,deslar,
                    descor,vigente,fijo,bnul,bnul,bnul,
                    grati,bnul,ir,bnul,bnul,ley,
                    bnul,bnul,bnul,sctr,bnul,dias1,
                    dias2,faltas,hextra,indem);*/
                  
                    ResultSet  auxres=conex.consultar("select * from conceptos where id_concepto='"+id_co+"'");
                    auxres.last();
                    int auxi=auxres.getRow();
                    if(auxi<1){
                    
                      
                    System.out.println(contador+" "+id_co+" "+id_ti_co+" "+cuenta1+" "+cuenta2+" "+monto+" "+deslar+"\t"+
                    descor+" "+vigente+" "+
                    grati+" "+ir+" "+ley+" "+"\t"+
                    sctr+" "+dias1+
                    dias2+" "+faltas+" "+hextra+" "+indem);
                    contador++;
                    }
           }
         }     
        }catch (Exception e){
            e.printStackTrace();
        }
    }//GEN-LAST:event_jbConsultarActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TablaDBF().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbConsultar;
    private javax.swing.JTable jtTablaDBF;
    private javax.swing.JTextField jtfConsulta;
    // End of variables declaration//GEN-END:variables
    
}
